package com.gengine.core.frame
{
	
	public class BaseTimer extends Object implements IBaseTimer
	{
		private var _isTimerQueue:Boolean = false;
		private var _running:Boolean = false;
		private var _id:int = 0;
		private var _isDelete:Boolean;
		private var _delay:Number;
		private var _repeatCountCache:Number;
		private var _repeatCount:Number;
		public var currentCount:int;
		private var _interval:int;
		private var _enterFrameHandler:Function;
		private var _completeHandler:Function;
		private static var _baseID:int = 0;
		
		public function BaseTimer(delay:Number, repeatCount:Number)
		{
			this._id = _baseID + 1;
			this.delay = delay;
			this.repeatCount = repeatCount;
			this._repeatCountCache = repeatCount;
		}
		
		public function get isTimerQueue() : Boolean
		{
			return this._isTimerQueue;
		}
		
		public function set isTimerQueue(isTimerQueue:Boolean) : void
		{
			this._isTimerQueue = isTimerQueue;
		}
		
		public function get type() : String
		{
			return "";
		}
		
		public function get id() : int
		{
			return this._id;
		}
		
		public function addListener(type:String, handler:Function) : void
		{
			if (type == TimerType.COMPLETE)
			{
				this._completeHandler = handler;
			}
			else if (type == TimerType.ENTERFRAME)
			{
				this._enterFrameHandler = handler;
			}
		}
		
		public function renderer(param1:int) : void
		{
			currentCount += 1;
			repeatCount = repeatCount - 1;
			if (this._enterFrameHandler is Function)
			{
				this._enterFrameHandler(this);
			}
			if (this.repeatCount <= 0)
			{
				this._isDelete = true;
				this.isTimerQueue = false;
				if (this._completeHandler is Function)
				{
					this._completeHandler(this);
				}
			}
		}
		
		public function get interval() : int
		{
			return this._interval;
		}
		
		public function set interval(param1:int) : void
		{
			this._interval = param1;
		}
		
		public function start() : void
		{
			this._running = true;
		}
		
		public function stop() : void
		{
			this._running = false;
			this.currentCount = 0;
		}
		
		public function set isDelete(isDelete:Boolean) : void
		{
			this._isDelete = isDelete;
		}
		
		public function get isDelete() : Boolean
		{
			return this._isDelete;
		}
		
		public function get running() : Boolean
		{
			return this._running;
		}
		
		public function get delay() : Number
		{
			return this._delay;
		}
		
		public function set delay(delay:Number) : void
		{
			this._delay = delay;
		}
		
		public function get repeatCount() : Number
		{
			return this._repeatCount;
		}
		
		public function set repeatCount(repeatCount:Number) : void
		{
			this._repeatCount = repeatCount;
		}
		
		public function reset() : void
		{
			this.repeatCount = this._repeatCountCache;
			this.currentCount = 0;
			this.isDelete = false;

		}
		
		public function dispose() : void
		{
			this._completeHandler = null;
			this._enterFrameHandler = null;
			this._isDelete = true;
			this._running = false;
			this._repeatCount = 0;
			this.isTimerQueue = false;
			this.currentCount = 0;
			
		}
	}
}
